package com.block.datapush.service.apipush.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * 大麦加密
 */

public class DaMaiAES {

    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return Base64.getEncoder().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.getDecoder().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        String cKey = "hxW7zT4BI9siImpE";
        String cSrc = "{\n" +
                "\"channel\": \"xsqb\",\n" +
                "\"phone\": \"19999999999\",\n" +
                "\"city\": \"杭州\",\n" +
                "\"name\": \"测试用户001\",\n" +
                "\"age\": \"22\",\n" +
                "\"loan\": \"3\",\n" +
                "\"loan_term\": 5,\n" +
                "\"loan_use\": 8,\n" +
                "\"professional\": 3,\n" +
                "\"credit_history\": 4,\n" +
                "\"zhima_credit\": 5,\n" +
                "\"education\": 7,\n" +
                "\"enterprise_type\": \"2\",\n" +
                "\"monthly_income\": 1,\n" +
                "\"salary_type\": 1,\n" +
                "\"enterprise_flow\": 1,\n" +
                "\"enterprise_license\": 1,\n" +
                "\"social_security_and_accumulation_fund\": 1,\n" +
                "\"micro_loan\": 1,\n" +
                "\"life_insurance\": 1,\n" +
                "\"house_property\": 5,\n" +
                "\"car_property\": 2\n" +
                "}";

        System.out.println(cSrc);
        String enString = DaMaiAES.Encrypt(cSrc, cKey);
        System.out.println("加密后的字串是1：" + enString);
        
        String DeString = DaMaiAES.Decrypt(enString, cKey);
        System.out.println("解密后的字串是1：" + DeString);

        SymmetricCrypto aes = SecureUtil.aes(cKey.getBytes());
        // 加密并 Base64 编码
        String encryptedBase64Data = aes.encryptBase64(cSrc, "UTF-8");
        System.out.println("加密后的数据2：" + encryptedBase64Data);
        // 加密
        String encryptHex = aes.encryptBase64(cSrc);
        System.out.println("加密后的数据2：" + encryptHex);

        // 解密
        String decryptStr = aes.decryptStr(encryptHex);
        System.out.println("解密后的数据2：" + decryptStr);


    }
}

    